﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace FinalProject
{
    public partial class WebForm21 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                InitPage();
            else
            {
                btnNopHoSo.Click += new EventHandler(btnNopHoSo_Click);
                btnLuu.Click += new EventHandler(btnLuu_Click);
            }
        }

        void btnLuu_Click(object sender, EventArgs e)
        {
            if (Session["UserName"] != null)
            {
                Utility.UserCurrent.InsertCongViecUngVien(GetCongViec(false));
            }
            else
            {
                Response.Redirect("Login.aspx?id=18&idNganhNghe=" + Request["id"]);
            }
        }

        void btnNopHoSo_Click(object sender, EventArgs e)
        {
            if (Session["UserName"] != null)
            {
                switch (Request["isNopHoSo"])
                {
                    case "true":
                        break;
                    case "false":
                        CongViecUngVien congViec = Utility.UserCurrent.GetCongViecUngVienByID(Request["idCongViecUngVien"]);
                        congViec.IsNopHoSo = true;
                        congViec.TinhTrang = "Đã ứng cử";
                        congViec.Ngay = DateTime.Today;
                        Utility.UserCurrent.UpdateCongViecUngVien(congViec);                        
                        break;
                    default:
                        Utility.UserCurrent.InsertCongViecUngVien(GetCongViec(true));
                        break;
                }                
            }
            else
            {                
                Response.Redirect("Login.aspx?id=18&idNganhNghe=" + Request["id"]);
            }
        }

        private CongViecUngVien GetCongViec(bool isNopHoSo)
        {
            try
            {
                CongViec congViec = Utility.getCongViecByID(Request["id"]);
                CongViecUngVien congViecUngVien = new CongViecUngVien("", congViec.ID, Utility.UserCurrent.StrEmpID, isNopHoSo, DateTime.Today, congViec.NoiLamViecID);
                return congViecUngVien;
            }
            catch { return null; }
        }

        private void InitPage()
        {
            try
            {
                string id = Request["id"];
                CongViec congViec = Utility.getCongViecByID(id);

                lblCapBac.Text = Utility.GetTenRootObject(Utility.LstChucVu, congViec.CapBacID);
                lblNoiLamViec.Text = Utility.GetTenRootObject(Utility.LstTinhThanh, congViec.NoiLamViecID);
                lblNgonNgu.Text = Utility.GetTenRootObject(Utility.LstNgoaiNgu, congViec.NgonNguID);
                lblNguoiLienHe.Text = congViec.NguoiLienHe;
                lblTenNganhNghe.Text = congViec.Ten;
                lblNgayBD.Text = congViec.NgayBD.ToShortDateString();
                lblNgayKT.Text = congViec.NgayKT.ToShortDateString();
                divMoTaCongViec.InnerHtml = congViec.MoTa;
                divYeuCauCongViec.InnerHtml = congViec.YeuCau;

                switch (Request["isNopHoSo"])
                {
                    case "true":
                        btnLuu.Visible = false;
                        btnNopHoSo.Visible = false;
                        break;
                    case "false":
                        btnLuu.Visible = false;
                        btnNopHoSo.Visible = true;
                        break;
                    default:
                        btnLuu.Visible = true;
                        btnNopHoSo.Visible = true;
                        break;
                }
            }
            catch { }
        }
    }
}
